<?php
/**
 * User: guo.hao
 * Date: 13-12-20
 * Time: am 11:13
 */
class Fun{
    //xml
    public static function xmlToArray($xml) {
        $array = (array)(simplexml_load_string($xml,'SimpleXMLElement', LIBXML_NOCDATA));
        foreach ($array as $key=>$item){
            $array[$key] = self::structToArray((array)$array[$key]);
        }
        return $array;
    }

    public static function structToArray($item) {
        if(!is_string($item)) {
            $item = (array)$item;
            foreach ($item as $key=>$val){
                $item[$key] = self::structToArray($val);
            }
        }
        return $item;
    }

    public static function p($var){
        CVarDumper::dump($var,10,true);
        echo '<br/>';
        echo '<br/>';
    }


    //get ip deny list
    public static function getIpDenyList(){
        $memcached = new Memcached();
        $memcached->addServer('127.0.0.1', 11211);
        $deny_ip = $memcached->get('deny_ip');
        if(!$deny_ip)    return self::setIpDenyList();
        $deny_ip = $memcached->get('deny_ip');
        return json_decode($deny_ip,true);
    }

    public static function setIpDenyList(){
        $memcached = new Memcached();
        $memcached->addServer('127.0.0.1', 11211);
        $memcached->delete('deny_ip');
        $deny_ip = DenyIp::model()->findAll();
        $deny_ip = json_encode(array_values(CHtml::listData($deny_ip,'ip','ip')));
        $memcached->add('deny_ip',$deny_ip,60*60*24);
        $deny_ip = $memcached->get('deny_ip');
        return json_decode($deny_ip,true);
    }
}